
var _myBrowser = function () {
    var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/rv:([\d.]+)\) like gecko/)) ? Sys.ie = s[1] :
        (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
            (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
                (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
                    (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
                        (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
    if (Sys.ie) {
        if (Sys.ie == 5.5) return 'IE5';
        if (Sys.ie == 6.0) return 'IE6';
        if (Sys.ie == 7.0) return 'IE7';
        if (Sys.ie == 8.0) return 'IE8';
        if (Sys.ie == 9.0) return 'IE9';
        if (Sys.ie == 10.0) return 'IE10';
        if (Sys.ie == 11.0) return 'IE11';
    };
    if (Sys.firefox) return 'Firefox';
    if (Sys.chrome) return 'Chrome';
    if (Sys.opera) return 'Opera';
    if (Sys.safari) return 'Safari';
};

var formatDate = function (format, str) {
    var errorEvent = function () {
        console.log('ExDate :非法事件格式' + str);
        return 'error';
    };
    var replaceDate = function () {

    }

    var formatStr = format || 'yyyy-MM-DD';
    if (format == '中' || format == 'zh') {
        formatStr = 'yyyy年M月D日';
    } else if (format == 'ZH') {
        formatStr = 'yyyy年MM月DD日';
    } else if (format == 'ZH:') {
        formatStr = 'yyyy年MM月DD日 HH时mm分ss秒';
    } else if (format == 'zh:') {
        formatStr = 'yyyy年M月D日 T h时m分s秒';
    } else if (format == '-') {
        formatStr = 'yyyy-MM-DD';
    } else if (format == '/') {
        formatStr = 'yyyy/MM/DD';
    } else if (format == '-:') {
        formatStr = 'yyyy-MM-DD HH:mm:ss';
    };
    if (str instanceof Date) {
        var date = str;
    } else {
        if (str || str === 0) {
            if (!isNaN(str)) {
                var date = new Date(parseInt(str));
            } else {
                str = str.replace(/年/g, '/').replace(/月/g, '/').replace(/日/g, '').replace(/时/g, ':').replace(/分/g, ':').replace(/秒/g, '');
                if (_myBrowser() == 'Chrome') {
                    str = str.replace(/\//g, '-');
                };
                if (_myBrowser() == 'IE8' || _myBrowser() == 'IE9' || _myBrowser() == 'IE10' || _myBrowser() == 'IE11') {
                    str = str.replace(/\-/g, '/');
                    str = str.replace(/\./g, '/');
                };
                var date = new Date(str);
            }
        } else {
            var date = new Date();
        };
    };

    if (formatStr == 'z') {
        date = new Date(date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate());
        formatStr = 'Z';
    };

    if (date == 'Invalid Date') {
        errorEvent();
        return 'error';
    };

    if (formatStr == 'Z') {
        return date.getTime();
    };

    var zeroize = function (value, length) {
        if (!length) length = 2;
        value = String(value);
        for (var i = 0, zeros = ''; i < (length - value.length); i++) {
            zeros += '0';
        }
        return zeros + value;
    };
    return formatStr.replace(/d{1,2}|D{1,2}|m{1,2}|yy(?:yy)|YY(?:YY)|([hHMstTZ]){1,2}/g, function ($0) {
        switch ($0) {
            case 'd':
                return date.getDate();
            case 'dd':
                return zeroize(date.getDate());
            case 'D':
                return date.getDate();
            case 'DD':
                return zeroize(date.getDate());
            case 'M':
                return date.getMonth() + 1;
            case 'MM':
                return zeroize(date.getMonth() + 1);
            case 'yy':
                return String(date.getFullYear()).substr(2);
            case 'yyyy':
                return date.getFullYear();
            case 'YY':
                return String(date.getFullYear()).substr(2);
            case 'YYYY':
                return date.getFullYear();
            case 'h':
                return date.getHours() % 12 || 12;
            case 'hh':
                return zeroize(date.getHours() % 12 || 12);
            case 'H':
                return date.getHours();
            case 'HH':
                return zeroize(date.getHours());
            case 'm':
                return date.getMinutes();
            case 'mm':
                return zeroize(date.getMinutes());
            case 's':
                return date.getSeconds();
            case 'ss':
                return zeroize(date.getSeconds());
            case 'tt':
                return date.getHours() < 12 ? 'am' : 'pm';
            case 'TT':
                return date.getHours() < 12 ? 'AM' : 'PM';
            case 'T':
                return date.getHours() < 12 ? '上午' : '下午';
            case 'Z':
                return date.getTime();
            default:
                return $0.substr(1, $0.length - 2);
        }
    })
}
module.exports = formatDate;